package com.iqiyi.pps.epg.core.utils;

import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.infrastructure.mail.MailService;
import com.iqiyi.pps.epg.core.model.common.MailHeader;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SendMailUtils {
    private static Logger logger = LoggerFactory.getLogger(SendMailUtils.class);

    public static void send(String title, String[] tos, String content) {
        // 标题添加环境标识
        String deployEnv = Configuration.getString("deploy.env");
        if (StringUtils.isNotBlank(deployEnv)) {
            title = "[" + deployEnv + "]" + title;
        }

//        StackTraceElement[] stack = (new Throwable()).getStackTrace();
//        StackTraceElement ste = stack[1];

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Calendar now = Calendar.getInstance();
//        content = "from: " + IPv4Util.getLocalHost() +
//            " " + ste.getClassName() + "." + ste.getMethodName() +
//            " " + ste.getLineNumber() +
//            ", time: " + sdf.format(now.getTime()) +
//            "<br /><br />" + content;

        sdf.applyPattern("yyyy-MM-dd");
        title += " " + sdf.format(now.getTime()); // 标题后添加今天的日期

        try {
            MailService mailservice = (MailService) ApplicationContextUtil.getBean(MailService.class);
            MailHeader mailHeader = new MailHeader();
            mailHeader.setTos(tos);
            mailHeader.setTitle(title);
            boolean flag = mailservice.sendMail(mailHeader, 0L, content);
            logger.info("[SendMailUtils][send][status=" + flag + "]");
        } catch (Exception e) {
            logger.info("[SendMailUtils][send][exception=" + e.getMessage() + "]");
        }
    }

    public static boolean sendMail(String title,String[] tos , String content){
        String deployEnv = Configuration.getString("deploy.env");
        if (StringUtils.isNotBlank(deployEnv)) {
            title = "[" + deployEnv + "]" + title;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Calendar now = Calendar.getInstance();
//        content = "from: " + IPv4Util.getLocalHost() +
//            " " + ste.getClassName() + "." + ste.getMethodName() +
//            " " + ste.getLineNumber() +
//            ", time: " + sdf.format(now.getTime()) +
//            "<br /><br />" + content;

        sdf.applyPattern("yyyy-MM-dd");
        title += " " + sdf.format(now.getTime()); // 标题后添加今天的日期
        boolean flag = false;
        try {
            MailService mailservice = (MailService) ApplicationContextUtil.getBean(MailService.class);
            MailHeader mailHeader = new MailHeader();
            mailHeader.setTos(tos);
            mailHeader.setTitle(title);
            flag = mailservice.sendMail(mailHeader, 0L, content);
            logger.info("[SendMailUtils][send][status=" + flag + "]");
        } catch (Exception e) {
            logger.error("[SendMailUtils][exception={}]",e);
        }
        return flag;
    }

}
